﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GGJ_DKG.Objects
{
    class Objeto2D
    {

        private Texture2D texture;
        /// <summary>
        /// Texture image for your object.
        /// </summary>
        /// <returns>Returns current texture being used.</returns>
        /// <param name="Set">Sends the texture to be used.</param>
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }


        private Color color;
        /// <summary>
        /// Color tint for your object.
        /// </summary>
        /// <returns>Returns current color tint for object.</returns>
        /// <param name="Set">Sends the current color Tint to be used.</param>
        public Color ObjectColor
        {
            get { return color; }
            set { color = value; }
        }



        private Vector2 position;
        /// <summary>
        /// Position for your object.
        /// </summary>
        /// <returns>Current position of game object.</returns>
        /// <param name="Set">Vector2 to define the position for object.</param>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }


        private Vector2 middlePoint;
        /// <summary>
        /// Defines the 0,0 of the game object in the middle of the texture.
        /// </summary>
        /// <returns>0,0 position of the object in the middle of the texture.</returns>
        /// <param name="Set">Defines middle position of the object</param>
        public Vector2 MiddlePoint
        {
            get { return middlePoint; }
            set { middlePoint = value; }
        }


        private Vector2 scaleVector;
        /// <summary>
        /// Scale factor in a vector used to define ingame alterations to your object.
        /// </summary>
        /// <returns></returns>
        /// <param name="Set"></param>
        public Vector2 ScaleVector
        {
            get { return scaleVector; }
            set { scaleVector = value; }
        }


        private float rotation;
        /// <summary>
        /// Rotation for the object.
        /// </summary>
        /// <returns></returns>
        /// <param name="Set"></param>
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }


        private float alpha;
        /// <summary>
        /// Opacity of the object.
        /// </summary>
        /// <returns></returns>
        /// <param name="Set"></param>
        public float Alpha
        {
            get { return alpha; }
            set { alpha = value; }
        }

        public bool canEdit;
        private bool visible;
        /// <summary>
        /// Alters wether or not the object will be drawn.
        /// </summary>
        /// <returns></returns>
        /// <param name="Set"></param>
        public bool Visible
        {
            get { return visible; }
            set { if(canEdit){visible = value; } }
        }


        private Rectangle collisionBounds;
        /// <summary>
        /// Collision controller for game objects.
        /// </summary>
        /// <returns></returns>
        /// <param name="Set"></param>
        public Rectangle CollisionBounds
        {
            get { return collisionBounds; }
            set { collisionBounds = value; }
        }
        // layer

        /// <summary>
        /// Construct for Object2D class.
        /// </summary>
        /// <param name="texture">Texture to be used when drawing your object.</param>
        public Objeto2D(Texture2D texture)
        {
            this.texture = texture;
            position = new Vector2(200 , 200);
            color = Color.White;
            rotation = 50;
            middlePoint = new Vector2(texture.Width/2 , texture.Height/2);
            scaleVector = new Vector2(1f , 1f);
            visible = true;
            alpha = 1;
            calculateRetangle();
        }

        public void calculateRetangle()
        {
            collisionBounds = new Rectangle( (int)(position.X - (middlePoint.X * scaleVector.X) ),
                (int)(position.Y - (middlePoint.Y * scaleVector.Y)), 
                (int)(texture.Width * scaleVector.X), 
                (int)(texture.Height * scaleVector.Y));
        }

        /// <summary>
        /// Tests collision between two game objects.
        /// </summary>
        /// <param name="object2D">Receives the object to be tested for collision</param>
        /// <returns>If true the two objects are colliding, otherwise will return false.</returns>
        public bool hitTestObject(Objeto2D object2D)
        {
            this.calculateRetangle();
            object2D.calculateRetangle();

            if (this.collisionBounds.Intersects(object2D.collisionBounds))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Draws your object on the screen
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (visible==true)
            {
                spriteBatch.Draw(texture, position, null, color * alpha, MathHelper.ToRadians(rotation), middlePoint, scaleVector, SpriteEffects.None, 0.5f);
                
            }

        }

    }
}
